class Admin::ActiveShopCategoriesController < AdminController
  before_action :set_active

  def index
    @active_shop_categories = @active.active_shop_categories.page(params[:page]).per(params[:per])
  end

  def new
    @active_shop_category = ActiveShopCategory.new
  end

  def create
    Rails.logger.error params
    @active_shop_category = ActiveShopCategory.new
    @active_shop_category.active_id = @active.id
    @active_shop_category.shop_category_id = params[:shop_category_id]
    @active_shop_category.sort = 0
    if @active_shop_category.save!
      Rails.logger.error @active_shop_category.errors
      redirect_to admin_active_active_shop_categories_path(@active), notice: "保存成功！"
    else
      redirect_to admin_active_active_shop_categories_path(@active), notice: "保存失败！"
    end
  end

  def edit
    @active_shop_category = ActiveShopCategory.find params[:id]
  end
  
  def update
    @active_shop_category = ActiveShopCategory.find params[:id]
    if @active_shop_category.update active_shop_category_params
      redirect_to admin_active_active_shop_categories_path(@active), notice: "保存成功！"
    else
      redirect_to admin_active_active_shop_categories_path(@active), notice: "保存失败！"
    end
  end
  
  def destroy
    @active_shop_category = ActiveShopCategory.find params[:id]
    if @active_shop_category.destroy
      redirect_to admin_active_active_shop_categories_path(@active), notice: "删除成功."
    else
      redirect_to admin_active_active_shop_categories_path(@active), notice: "删除失败."
    end
  end

  private

  def set_active
    @active = Active.find params[:active_id]
  end

  def active_shop_category_params
    params.require(:active_shop_category).permit(:active_id, :shop_category_id, :sort)
  end
end
